package command;

import entity.Student;

import java.util.List;
import java.util.Scanner;

public class UpdateCommand implements Command{

    private final Scanner scanner = new Scanner(System.in);

    private Student _student = new Student();
    @Override
    public void execute(List<Student> students) throws InterruptedException {
        int id;
        while(true){
            System.out.print("请输入要修改的学生的学号：");
            String line = scanner.nextLine();
            try{
                id = Integer.parseInt(line);
                break;
            }catch(Exception e){
                System.err.println("请输入一个数字！");
                Thread.sleep(100);
            }
        }

        Student student = null;
        for(Student s : students){
            if(s.getId() == id){
                student = s;
                break;
            }
        }
        if(student == null){
            System.err.println("没有找到该学生！");
            return;
        }

        _student = student;
        System.out.print("请输入要修改的学生的姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入要修改的学生的性别：");
        String gender = scanner.nextLine();
        students.remove(student);
        students.add(student.getId()-1,new Student(id,name,gender));
    }

    @Override
    public void undo(List<Student> students) {
        Student student = students.remove(_student.getId()-1);
        students.add(_student.getId()-1,_student);
        System.out.println("成功撤销修改操作！修改的学生信息是:"+student);
    }
}
